package org.jakim.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jakim.entity.AdditionaInfo;
import org.jakim.entity.Education;
import org.jakim.entity.Language;
import org.jakim.entity.MotherLanguage;
import org.jakim.entity.PersonalSkill;
import org.jakim.entity.WorkExperience;
import org.jakim.utils.CLevels;

/**
 * Servlet implementation class AddInfo
 */
@WebServlet("/AddInfo")
public class AddInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddInfo() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int numberCVWizardStep = Integer.valueOf(request
				.getParameter("numberCVWizardStep"));

		switch (numberCVWizardStep) {
		case 1:
			this.addWorkExperience(request);
			response.sendRedirect("cvwizard/jobhistorywizard.jsp");
			break;
		case 2:
			this.addEducation(request);
			response.sendRedirect("cvwizard/educationwizard.jsp");
			break;
		case 3:
			this.addPersonalSkills(request);
			response.sendRedirect("cvwizard/personalskillswizard.jsp");
			break;
		case 4:
			this.addLanguage(request);
			response.sendRedirect("cvwizard/languageswizard.jsp");
			break;
		case 5:
			this.addOthers(request);
			response.sendRedirect("cvwizard/additionalinformationwizard.jsp");
			break;
		}
	}

	@SuppressWarnings("unchecked")
	private void addWorkExperience(HttpServletRequest request) {
		HttpSession httpSeesion = request.getSession();
		List<WorkExperience> listWorkExperience = null;
		Object objWrokExperience = httpSeesion
				.getAttribute("attrWrokExperience");

		if (objWrokExperience == null) {
			listWorkExperience = new ArrayList<WorkExperience>();
			httpSeesion.setAttribute("attrWrokExperience", listWorkExperience);
		} else {
			if (objWrokExperience instanceof List<?>) {
				listWorkExperience = (List<WorkExperience>) objWrokExperience;
			}
		}

		WorkExperience work = new WorkExperience();

		int monthNumber = Integer.parseInt(request
				.getParameter("workExperienceDatesStartMonth"));
		int yearNumber = Integer.parseInt(request
				.getParameter("workExperienceDatesStartYear"));
		Calendar date = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
		date.set(yearNumber, monthNumber, 1);
		work.setWorkStartDate(date.getTime());

		boolean isOnwards = Boolean.parseBoolean(request
				.getParameter("isOnwards"));
		if (isOnwards) {
			work.setWorkEndDate(null);
		} else {
			monthNumber = Integer.parseInt(request
					.getParameter("workExperienceDatesStartMonth"));
			yearNumber = Integer.parseInt(request
					.getParameter("workExperienceDatesStartYear"));
			date.set(yearNumber, monthNumber, 1);
			work.setWorkEndDate(date.getTime());
		}

		work.setEmployerAddress(request.getParameter("workEmployerAddress"));
		work.setEmployerFirstName(request.getParameter("workEmployerName"));
		work.setEmployerSirName(request.getParameter("workEmployerSirName"));
		work.setWorkBusinessSector(request.getParameter("workType"));
		work.setWorkMainActivities(request.getParameter("workMainActiviteis"));
		work.setWorkOccupation(request.getParameter("workOccupation"));
		listWorkExperience.add(work);
		/*
		 * TODO: Finish the work setting object Create and invoke a method to
		 * add the new object to ta DB
		 */

		// request.getSession().setAttribute("attrWrokExperience",
		// listWorkExperience);
	}

	@SuppressWarnings("unchecked")
	private void addEducation(HttpServletRequest request) {
		HttpSession httpSeesion = request.getSession();
		List<Education> listEducationAndSkills = null;

		Object objEducationAndSkills = httpSeesion
				.getAttribute("attrEducationAndSkills");

		if (objEducationAndSkills == null) {
			listEducationAndSkills = new ArrayList<Education>();
			httpSeesion.setAttribute("attrEducationAndSkills",
					listEducationAndSkills);
		} else {
			if (objEducationAndSkills instanceof List<?>) {
				listEducationAndSkills = (List<Education>) objEducationAndSkills;
			}
		}

		Education education = new Education();

		int monthNumber = Integer.parseInt(request
				.getParameter("educationDatesStartMonth"));
		int yearNumber = Integer.parseInt(request
				.getParameter("educationDatesStartYear"));
		Calendar date = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
		date.set(yearNumber, monthNumber, 1);

		education.setEduStartDate(date.getTime());

		boolean isOnwards = Boolean.parseBoolean(request
				.getParameter("isOnwards"));

		if (isOnwards) {
			education.setEduEndDate(null);
		} else {
			monthNumber = Integer.parseInt(request
					.getParameter("educationDatesEndMonth"));
			yearNumber = Integer.parseInt(request
					.getParameter("educationDatesEndYear"));
			date.set(yearNumber, monthNumber, 1);
			education.setEduEndDate(date.getTime());
		}

		education.setEduQualificationTitle(request
				.getParameter("educationQualificationTitle"));
		education.setEduOccupationalSkills(request
				.getParameter("educationCoveredSkills"));
		education.setEduOrganisationName(request
				.getParameter("educationOrganisationName"));
		education.setEduOrganisationType(request
				.getParameter("educationOrganisationType"));
		education.setEduStreet(request
				.getParameter("educationOrganisationStreet"));
		education.setEduCity(request.getParameter("educationOrganisationCity"));
		education.setEduPostalCode(Integer.valueOf(request
				.getParameter("educationOrganisationPostalCode")));
		education.setEduCountry(request
				.getParameter("educationOrganisationCountry"));
		education.setEduClassificationLevel(request
				.getParameter("educationClassificationLevel"));
		education.setEduEducationField(request.getParameter("educationField"));

		listEducationAndSkills.add(education);
	}

	@SuppressWarnings("unchecked")
	private void addLanguage(HttpServletRequest request) {
		HttpSession httpSeesion = request.getSession();
		List<Language> listLanguage = null;
		List<MotherLanguage> listMotherLanguage = null;
		
		Object objLanguage = httpSeesion.getAttribute("attrLanguages");
		Object objMotherLanguage = httpSeesion.getAttribute("attrMotherLanguage");

		if(objLanguage != null && objLanguage instanceof List<?>){
			listLanguage = (List<Language>) objLanguage;
		}else{
			listLanguage = new ArrayList<Language>();
		}
		
		if(objMotherLanguage != null && objMotherLanguage instanceof List<?>){
			listMotherLanguage = (List<MotherLanguage>) objMotherLanguage;
	    }else{
	    	listMotherLanguage = new ArrayList<MotherLanguage>();
	    }
		
		MotherLanguage motherLang = new MotherLanguage();
		motherLang.setLanguageName(request.getParameter("motherLanguageName"));
		listMotherLanguage.add(motherLang);
		
		Language language = new Language();
		language.setLanguageName(request.getParameter("languageName"));
		language.setLanguageListening(CLevels.valueOf(request.getParameter("languageListening")));
		language.setLanguageReading(CLevels.valueOf(request.getParameter("languageReading")));
		language.setLanguageInteraction(CLevels.valueOf(request.getParameter("languageInteraction")));
		language.setLanguageProduction(CLevels.valueOf(request.getParameter("languageProduction")));
		language.setLanguageWriting(CLevels.valueOf(request.getParameter("languageWriting")));
		listLanguage.add(language);
		
		httpSeesion.setAttribute("attrLanguages", listLanguage);
		httpSeesion.setAttribute("attrMotherLanguage", listMotherLanguage);
		
	}

	private void addPersonalSkills(HttpServletRequest request) {
		HttpSession httpSeesion = request.getSession();
		PersonalSkill personalSkills = new PersonalSkill();

		personalSkills.setSkillSocial(request.getParameter("skillSocial"));
		personalSkills.setSkillOranisational(request
				.getParameter("skillOrganizational"));
		personalSkills
				.setSkillTechnical(request.getParameter("skillTechnical"));
		personalSkills.setSkillArtistic(request.getParameter("skillArtistic"));
		personalSkills.setSkillComputer(request.getParameter("skillComputer"));
		personalSkills.setSkillOther(request.getParameter("skillOther"));

		for (String license : request.getParameterValues("drivingLincense")) {
			personalSkills.addDrivingLicense(license);
		}

		httpSeesion.setAttribute("attrPersonalSkills", personalSkills);
	}

	private void addOthers(HttpServletRequest request) {
		HttpSession httpSeesion = request.getSession();
		AdditionaInfo additionalInfo = null;

		Object objAdditionalInfo = httpSeesion
				.getAttribute("attrAdditionalInfo");

		if (objAdditionalInfo == null) {
			additionalInfo = new AdditionaInfo();
		} else {
			if (objAdditionalInfo instanceof AdditionaInfo) {
				additionalInfo = (AdditionaInfo) objAdditionalInfo;
			}
		}

		additionalInfo
				.setAdditionalInfo(request.getParameter("additionalInfo"));
		additionalInfo.setAnnexes(request.getParameter("additionalAnnex"));

		httpSeesion.setAttribute("attrAdditionalInfo", additionalInfo);
	}
}
